﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;

namespace Utils.MVC
{
    public static class PageExtension
    {

        #region 输出JavaScript

        public static string OutputJS(this ViewPage page,string selector)
        {
            if (selector == "")
                selector = "form";
            StringBuilder sb=new StringBuilder();
            sb.Append("<script type=\"text/javascript\">\r\n");
            sb.Append(page.ShowMessage());
            sb.Append(page.ClientValidations().ToScripts(selector));
            sb.Append("\r\n</script>");
            return sb.ToString();
        }
        public static string OutputJS(this ViewPage page)
        {
            return page.OutputJS("form");
        }

        #endregion


        public static string RegUpload(this ViewPage page,string ext,string url,string bid,string tid,string errMes)
        {

            string t =
                @"
<script type=""text/javascript"">
$(document).ready(function() {{
        new AjaxUpload('#{0}', {{
                action: '{1}',
                responseType: ""html"",
                onSubmit: function(file, ext) {{
                if (!(ext && /^({2})$/.test(ext))) {{
                        // extension is not allowed  
                        alert('{4}');
                        // cancel upload  
                        return false;
                    }}
                }},
                onComplete: function(file, response) {{ $(""#{3}"").html(response); }}
            }});
        }});
	</script>
";
            return string.Format(t, bid, url, ext, tid, errMes);
        }


        #region 显示提示信息
        private const string MesFormatString = "OSShowMes({0});";
        private const string KeyName = "jaMes";
        /// <summary>
        /// 添加提示信息的辅助方法
        ///     执行OSShowMes(mes)
        /// </summary>
        /// <param name="page"></param>
        /// <param name="mes">提示信息</param>
        public static void AddMessage(this ViewPage page, string mes)
        {
            if (mes.IsNullOrEmpty())
                return;
            page.ViewData.ModelState.AddModelError(KeyName,mes);
        }
        /// <summary>
        /// 输出提示信息js
        /// </summary>
        /// <param name="page"></param>
        public static string ShowMessage(this ViewPage page)
        {
            StringBuilder sb = new StringBuilder();
            if (!page.ViewData.ModelState.ContainsKey(KeyName))
                return "";
            foreach (var s in page.ViewData.ModelState[KeyName].Errors)
            {
                sb.Append((s.ErrorMessage+"\r\n").ToJson());
                sb.Append("+");
            }

            string script = sb.ToString();
            script = script.Remove(script.Length - 1);
            script = string.Format(MesFormatString, script);
            return script;
        }
        #endregion
    }
}
